//
//  NewsPushController.m
//  easystudy
//
//  Created by 七 on 16/9/4.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "NewsPushController.h"
#import "ContentViewController.h"
#import "NewPushCell.h"
@interface NewsPushController ()<UITableViewDelegate,UITableViewDataSource>
{
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UILabel *lineLbl;
    __weak IBOutlet UIButton *titleBtn;
    __weak IBOutlet UITableView *dataTable;
    IBOutlet UIView *BGVIew;
    NSMutableArray *dataList;
    
}
@end

@implementation NewsPushController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataList = [NSMutableArray array];
    
    navView.backgroundColor = KKColor_DarkGray_White;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    titleBtn.backgroundColor = KKColor_DarkGray_White;
    BGVIew.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    
    [WebService getPushList:@{} WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"errcode"] integerValue] == 0) {
                [dataList addObjectsFromArray:result[@"result"][@"article_psuh_list"][@"value"]];
                [dataTable reloadData];
            }
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource && Delegate

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"NewPushCell";
    NewPushCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        [tableView registerNib:[UINib nibWithNibName:@"NewPushCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = dataList[indexPath.row];
    [cell.iconIV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",FileURL,dict[@"a_picture"]]] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
    cell.titleLbl.text = [dict[@"a_title"] convertToString];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dict[@"psuh_createtime"] longLongValue]];
    cell.timeLbl.text = [date formattedDateDescription];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = dataList[indexPath.row];
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    contentVC.dataInfo = @{@"a_id":item[@"psuh_aid"]};
    contentVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:contentVC animated:YES];
}

@end
